﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace ExceptionParser
{
  
    public partial class Main : Form
    {

        public ExceptionParser ep = new ExceptionParser();
        public Main()
        {
            InitializeComponent();
            ep.Over += this.showResults;
        }

        /// <summary>
        /// Method that shows the Results form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void showResults(ParserEventArgs e)
        {
            Program.Try_s = e.Try_s;
            Program.Catch_s = e.Catch_s;
            Results res = new Results();
            res.ShowDialog();
        }

        /// <summary>
        /// Occurs, when the "Exit" button is pressed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonExit_Click(object sender, EventArgs e) 
        {
            if (MessageBox.Show("Do you really want to exit?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        /// <summary>
        /// Occurs, when the "Clear" button is pressed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonClear_Click(object sender, EventArgs e)
        {
            if (textBoxCsFile.Text != "")
            {
                if (MessageBox.Show("Do you really want clear all the text?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    textBoxCsFile.Clear();
                    labelName.Visible = false;
                }
            }
        }

        /// <summary>
        /// Occurs, when the "Open .cs file" button is pressed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOpen_Click(object sender, EventArgs e) 
        {
            if ((openFileDialog.ShowDialog() == DialogResult.OK) && (File.Exists(openFileDialog.FileName)))
            {
                if ((textBoxCsFile.Text!="") && (MessageBox.Show("Do you really want clear all the text and open new file?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
                {
                    textBoxCsFile.Clear();
                    textBoxCsFile.Text = File.ReadAllText(openFileDialog.FileName);
                }
                else
                    if(textBoxCsFile.Text=="")
                         textBoxCsFile.Text = File.ReadAllText(openFileDialog.FileName);
                Program.LastPath = openFileDialog.FileName;
                labelName.Text = Path.GetFileName(Program.LastPath);
                labelName.Location = new System.Drawing.Point(textBoxCsFile.Location.X + textBoxCsFile.Size.Width - labelName.Size.Width - 20, labelName.Location.Y);
                labelName.Visible = true;
            }
        }

        /// <summary>
        /// Occurs, when the "Save .cs file" button is pressed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSave_Click(object sender, EventArgs e) //Save .CS file
        {
            if(Program.LastPath!="")
               saveFileDialog.FileName = Path.GetFileName(Program.LastPath);
            if ((saveFileDialog.ShowDialog() == DialogResult.OK) && (saveFileDialog.FileName != ""))
            {
                File.WriteAllText(saveFileDialog.FileName, textBoxCsFile.Text);
                MessageBox.Show("File saved!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Program.LastPath = saveFileDialog.FileName;
                labelName.Text = Path.GetFileName(Program.LastPath);
                labelName.Location = new System.Drawing.Point(textBoxCsFile.Location.X + textBoxCsFile.Size.Width - labelName.Size.Width - 20, labelName.Location.Y);
                labelName.Visible = true;
            }
        }

        /// <summary>
        /// Occurs, when the "About" button is pressed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonAbout_Click(object sender, EventArgs e) //Open the information form
        {
            AboutBox Ab = new AboutBox();
            Ab.ShowDialog();
        }

        /// <summary>
        /// Occurs, when the "Parse" button is pressed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonParse_Click(object sender, EventArgs e) //Launch parser
        {
            if (textBoxCsFile.Text != "")
            {
                ep.ExceptionSearch(textBoxCsFile.Lines);
            }
        }

        /// <summary>
        /// Occurs, when the Main form size is changed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_SizeChanged(object sender, EventArgs e)
        {
            labelName.Location = new System.Drawing.Point(textBoxCsFile.Location.X + textBoxCsFile.Size.Width - labelName.Size.Width - 20, labelName.Location.Y);
        }

    }
 
}
